package com.bio;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TimeServer {
    public static void main(String[] args) throws IOException {
        int port = 8080;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("The time server is tart at port:" + port);
        TimeServerExecutePoll executePoll = new TimeServerExecutePoll(4,3);
        Socket socket = null;
        while (true) {
            socket = serverSocket.accept();
            //new Thread(new TimeServerHandler(socket)).start();
            executePoll.executeTask(new TimeServerHandler(socket));
        }
    }
}

//线程池
class TimeServerExecutePoll {
    private ExecutorService executor;

    public TimeServerExecutePoll(int maxPollSize, int queueSize) {
        executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                maxPollSize, 120L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize));
    }

    public void executeTask(Runnable runnable){
        executor.execute(runnable);
    }
}

class TimeServerHandler implements Runnable {

    private Socket socket;

    public TimeServerHandler(Socket socket) {
        try {
            socket.setTcpNoDelay(true);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            String currentTime = null;
            String body = null;
            while (true) {
                body = reader.readLine();
                if (body == null) {
                    break;
                }
                System.out.println(Thread.currentThread().getName() + ":The timer server receive order: " + body);
                currentTime = "date".equalsIgnoreCase(body) ? String.valueOf(System.currentTimeMillis()) : "bad order";
                writer.println(currentTime);
                //writer.println(currentTime);
                //writer.println(currentTime);
                writer.flush();
            }
        } catch (Exception e) {

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null) {
                writer.flush();
                writer.close();
                writer = null;
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
